# -*- coding:utf-8 -*-
from flask import jsonify, request, abort
from . import app
from com.zzh.data_center.dumpData import ExportFromMongo
from com.zzh.dao_v2.itemDao import ItemDao


@app.route('/query_data', methods=['POST', 'GET'])
def query_data():
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    data, total = ExportFromMongo().query(pageIndex=pageIndex, pageSize=pageSize)
    if data:
        return jsonify({"total": total, "info": data, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/update_status', methods=['POST'])
def update_status():
    if not request.json:
        abort(400)
    try:
        objIds = request.json['idArray']
    except:
        return jsonify({"message": {"code": 0, "status": "需要参数idArray"}})
    result = ExportFromMongo().update(objIds)

    if result:
        return jsonify({"idArray": objIds, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/insertItemAndUpdateMongo', methods=['POST'])
def insert_item_mongo():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    try:
        objId = request.json['mid']
    except:
        return jsonify({"message": {"code": 0, "status": "需要参数objId"}})

    info = {
        'deptId': request.json.get('deptId', 0),
        'itemHandlerName': request.json.get('itemHandlerName', ''),
        'itemHandlerPhone': request.json.get('itemHandlerPhone', ''),
        'itemTitle': request.json.get('itemTitle', ''),
        'itemPriceRange': request.json.get('itemPriceRange', ''),
        'itemContent': request.json.get('itemContent', ''),
        'itemUrl': request.json.get('itemUrl', ''),
        'itemDeadline': request.json.get('itemDeadline', None),
        'itemPublishdate': request.json.get('itemPublishdate', None),
        'itemAddress': request.json.get('itemAddress', ''),
        'itemStatus': request.json.get('itemStatus', 0),
        'serviceId': request.json.get('serviceId', 0),
        'itemEnterPerson': request.json.get('itemEnterPerson', ''),
        'itemEnterTime': request.json.get('itemEnterTime', None),
        'checkStatusOne': request.json.get('checkStatusOne', 0),
        'checkRemarkOne': request.json.get('checkRemarkOne', ''),
        'checkPersonOne': request.json.get('checkPersonOne', ''),
        'checkTimeOne': request.json.get('checkTimeOne', None),
        'checkStatusTwo': request.json.get('checkStatusTwo', 0),
        'checkRemarkTwo': request.json.get('checkRemarkTwo', ''),
        'checkTimeTwo': request.json.get('checkTimeTwo', None),
        'checkPersonTwo': request.json.get('checkPersonTwo', '')
    }

    rs, data = ItemDao().insertItem(info)
    if info:
        us = ExportFromMongo().update([objId])
        newId = ItemDao().getMaxId()
        return jsonify({"id": newId, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
